const express = require("express");
const { User, sequelize } = require("../../models");
const { success, fail } = require("../../utils/response");
const { QueryTypes } = require("sequelize");

const router = express.Router();

// 统计-用户性别
router.get("/sex", async (req, res) => {
  try {
    const man = await User.count({ where: { sex: 1 } });
    const woman = await User.count({ where: { sex: 2 } });
    const secret = await User.count({ where: { sex: 0 } });
    success(res, "获取性别统计成功", [
      { name: "男", value: man },
      { name: "女", value: woman },
      { name: "保密", value: secret },
    ]);
  } catch (error) {
    fail(res, error);
  }
});
// 统计每月注册用户数量
router.get("/registUser", async (req, res) => {
  try {
    const data = await sequelize.query(
      'SELECT DATE_FORMAT(createdAt, "%Y-%m") AS createTime, COUNT(*) AS count FROM `Users` GROUP BY createTime ORDER BY createTime ASC',
      { type: QueryTypes.SELECT }
    );

    success(res, "获取每月注册用户数量成功", {
      list: data,
    });
  } catch (error) {
    fail(res, error);
  }
});

module.exports = router;
